package com.tiantianfresh.view.controller;

import com.tiantianfresh.model.pojo.AJAXResult;
import com.tiantianfresh.model.pojo.Order;
import com.tiantianfresh.model.pojo.ShoppingCart;
import com.tiantianfresh.view.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author FANGAO
 * @date 2021/12/1 - 上午 11:56
 */
@Controller
@RequestMapping("/shoppingcart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @ResponseBody
    @RequestMapping("/queryShoppingCartAll")
    public Object ShoppingCartCommodityAll(@RequestParam(required = false,defaultValue = "0") Integer userid){
        AJAXResult result = new AJAXResult();
        List<ShoppingCart> shoppingCartList = shoppingCartService.queryShoppingCartAll(userid);
        if(shoppingCartList!=null){
            result.setData(shoppingCartList);
            result.setSuccess(true);
        }else{
            result.setSuccess(false);
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("/deleteShoppingCart")
    public Object ShoppingCartDeleteShoppingCart(Integer id){
        AJAXResult result = new AJAXResult();
        result.setSuccess(shoppingCartService.deleteShoppingCart(id));
        return result;
    }

    @ResponseBody
    @RequestMapping("/deleteShoppingCarts")
    public Object ShoppingCartDeleteShoppingCartS(@RequestParam(value = "scids[]") Integer[] scids){
        System.out.println(Arrays.toString(scids));
        AJAXResult result = new AJAXResult();
        Map<String,Integer[]> map = new HashMap<>();
        map.put("scids",scids);
        result.setSuccess(shoppingCartService.deleteShoppingCarts(map));
        return result;
    }
    @ResponseBody
    @RequestMapping("/updateQuantityup")
    public Object updateQuantityup(Integer id){
        AJAXResult result = new AJAXResult();
        Map<String,Integer> map = new HashMap<>();
        map.put("id",id);
        result.setSuccess(shoppingCartService.updateQuantityup(map));
        return result;
    }
    @ResponseBody
    @RequestMapping("/updateQuantitydow")
    public Object updateQuantitydow(Integer id){
        AJAXResult result = new AJAXResult();
        Map<String,Integer> map = new HashMap<>();
        map.put("id",id);
        result.setSuccess(shoppingCartService.updateQuantitydow(map));
        return result;
    }
    @ResponseBody
    @RequestMapping("/updeatState")
    public Object updeatState(Integer id,Integer state){
        AJAXResult result = new AJAXResult();
        Map<String,Integer> map = new HashMap<>();
        map.put("id",id);
        map.put("state",state);
        result.setSuccess(shoppingCartService.updeatState(map));
        return result;
    }
    @ResponseBody
    @RequestMapping("/addOrder")
    public Object addOrder(Integer userid,Double countmoney){
        AJAXResult result = new AJAXResult();
        Map<String,Object> map = new HashMap<>();
        map.put("userid",userid);
        map.put("countmoney",countmoney);
        Order order = shoppingCartService.addOrder(map);
        if (order!=null){
            result.setData(order);
            result.setSuccess(true);
        }else{
            result.setSuccess(false);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("/initializationState")
    public Object initializationState(Integer userid){
        AJAXResult result = new AJAXResult();
        Map<String,Integer> map = new HashMap<>();
        map.put("userid",userid);
        result.setSuccess(shoppingCartService.initializationState(map));
        return result;
    }
}
